package com.hoyin.viewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;

import com.hoyin.viewtest.dependencies.layout.AnotherRightFragment;
import com.hoyin.viewtest.dependencies.layout.RightFragment;

public class FragmentActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment);
		findViewById(R.id.left_fragment_button).setOnClickListener(view -> replaceFragment(new AnotherRightFragment()));
		//replaceFragment(new RightFragment());
	}

	private void replaceFragment(Fragment fragment) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.replace(R.id.right_fragment, fragment);
		transaction.addToBackStack(null);               //addToBackStack()模仿返回栈的效果
		transaction.commit();
	}
}